/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.homullus;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import org.lwjgl.LWJGLException;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileFilter;
import org.homullus.SkeletonLoaders.XMLSkeletonLoader;

/**
 *
 * @author Gary
 */
public class Main extends javax.swing.JFrame
{

    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem wireframeMenuItem;
    private javax.swing.JMenuItem frontViewMenuItem;
    private javax.swing.JMenuItem leftViewMenuItem;
    private javax.swing.JMenuItem backViewMenuItem;
    private javax.swing.JMenuItem rightViewMenuItem;
    private javax.swing.JMenuBar jMenuBar1;
    private EditorCanvas EditorPanel;
    private ControlPanel controlPanel;
    private JFileChooser fileOpener;

    public static void main(String[] args)
    {
         java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Main m = new Main();
                m.setVisible(true);
                m.Init();
            }
        });
    }

    public Main()
    {
        try
        {
            initComponents();
        }
        catch (LWJGLException ex)
        {
            System.out.println("LWJGL error");
        }
    }


    public void Init()
    {
        Thread t = new Thread(EditorPanel);
        t.start();
    }

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
        //TODO: change Loading to singleton and allow plugin
        
        FileFilter xmlfilter = new FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.isDirectory() || (f.isFile() && f.getName().contains("xml"));
            }

            @Override
            public String getDescription() {
                return "Xml Skeleton (.xml)"; 
            }
        };
        FileFilter txtfilter = new FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.isDirectory() || (f.isFile() && f.getName().contains("txt"));
            }

            @Override
            public String getDescription() {
                return "Text Skeleton (.txt)";
            }
        };
        fileOpener.addChoosableFileFilter(xmlfilter);
        //fileOpener.addChoosableFileFilter(txtfilter);
        fileOpener.setAcceptAllFileFilterUsed(false);
        fileOpener.showOpenDialog(null);
        FileFilter selected = fileOpener.getFileFilter();

        Skeleton Loadedskel = null;
        if (selected == xmlfilter)
        {
            Loadedskel = (new XMLSkeletonLoader()).Load(fileOpener.getSelectedFile().getAbsolutePath());
            //Bone b = Loadedskel.GetBone();
            //Loadedskel.RotateBone(b, 1 , 0);
            //Loadedskel.RotateBone(b, 1 , 0);
        }
        controlPanel.setSkeleton(Loadedskel);
        EditorPanel.SetSkeleton(Loadedskel);
    }

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {


        fileOpener.showOpenDialog(null);
    }

    private void jMenu3ActionPerformed(java.awt.event.ActionEvent evt) {
        fileOpener.showOpenDialog(null);

        Skeleton skel = new Skeleton();
        skel.LoadSkeleton("Skeletons\\cohSkeleton.xml");
        EditorPanel.SetSkeleton(skel);
    }


    @SuppressWarnings("unchecked")
    private void initComponents() throws LWJGLException {

        fileOpener = new JFileChooser();
        EditorPanel = new EditorCanvas();
        controlPanel = new ControlPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new JMenuItem();
        jMenuItem2 = new JMenuItem();
        wireframeMenuItem = new JMenuItem();
        frontViewMenuItem = new JMenuItem();
        leftViewMenuItem = new JMenuItem();
        backViewMenuItem = new JMenuItem();
        rightViewMenuItem = new JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(0, 0, 0));
        
        EditorPanel.setBackground(new java.awt.Color(0, 0, 0));

        controlPanel.setPreferredSize(new Dimension(436,649));

        jMenu1.setText("File");

        jMenuItem1.setText("Open Skeleton");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Export Skeleton");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        wireframeMenuItem.setText("Toggle Wireframe");
        wireframeMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        wireframeMenuItem.addActionListener(new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                EditorPanel.toggleWireframe();
            }
        });

        frontViewMenuItem.setText("View Front");
        frontViewMenuItem.addActionListener(new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                EditorPanel.setViewRotation(0, 0);
            }
        });
        leftViewMenuItem.setText("View Right");
        leftViewMenuItem.addActionListener(new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                EditorPanel.setViewRotation(0, 90);
            }
        });
        backViewMenuItem.setText("View Back");
        backViewMenuItem.addActionListener(new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                EditorPanel.setViewRotation(0, 180);
            }
        });
        rightViewMenuItem.setText("View Left");
        rightViewMenuItem.addActionListener(new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                EditorPanel.setViewRotation(0, 270);
            }
        });
        jMenu2.add(wireframeMenuItem);
        jMenu2.add(frontViewMenuItem);
        jMenu2.add(leftViewMenuItem);
        jMenu2.add(backViewMenuItem);
        jMenu2.add(rightViewMenuItem);
        
        jMenuBar1.add(jMenu2);

        

        setJMenuBar(jMenuBar1);


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(controlPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(EditorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 760, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(EditorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 671, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(controlPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }

}
